package mdm.com.web.shiro;


import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import mdm.com.web.dao.UserMapper;
import mdm.com.web.entity.User;
import mdm.com.web.entity.Wuser;


public class UserRealm extends AuthorizingRealm {
    private final static Logger LOGGER = LoggerFactory.getLogger(UserRealm.class);

    @Autowired
    private UserMapper userMapper;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
       return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        LOGGER.info("-----------------登录认证------------{}", authenticationToken);
        try {
            UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
            String username = token.getUsername();
            String type=token.getHost();
            LOGGER.info("-----------------登录认证状态 type------------"+type);
            if(type.equals("web")) {
                Wuser user=userMapper.selectByWusername(username);
                if (StringUtils.isNotEmpty(username)) {
                    return new SimpleAuthenticationInfo(username, user.getPassword(),"");
                }
            }
            if(type.equals("app")) {
                User u=userMapper.selectByUsername(username);
                if (StringUtils.isNotEmpty(username)) {
                    return new SimpleAuthenticationInfo(username, u.getcPassword(),"");
                }
            }
            return null;
        } catch (Exception e) {
            LOGGER.error("", e);
            return null;
        }
    }
}
